package com.projects.android.bus;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.util.Log;

import com.projects.android.dto.OrderDetailDTO;
import com.projects.android.utils.Utils;

public class OrderDetailBUS {
	public static ArrayList<OrderDetailDTO> selectByIDOrder(int idorder) {
		ArrayList<OrderDetailDTO> myData = null;
		try {
			Map<String, String> mapUser = new HashMap<String, String>();
			mapUser.put("idorder", String.valueOf(idorder));
			String url = Utils.buildURL(Utils.SERVICE_URI
					+ "/OrderDetail_SelectByIDOrder", mapUser);
			
			// Send GET request to <service>/GetPlates
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet request = new HttpGet(new URI(url));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			HttpResponse response = httpClient.execute(request);
			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			Log.i("OrderDetailBUS selectByIDOrder", "Response: " + result);

			if (result != "") {
				// Init array objects
				JSONArray arrData = new JSONArray(result);
				myData = new ArrayList<OrderDetailDTO>();
				// Loop for all reveived objects
				for (int i = 0; i < arrData.length(); i++) {
					JSONObject data = (JSONObject) arrData.get(i);
					OrderDetailDTO dto = new OrderDetailDTO();
					dto.initObject(data);
					myData.add(dto);
				}
			}
		} catch (Exception e) {
			Log.e("OrderDetailBUS selectByIDOrder", e.toString());
		}
		return myData;
	}
	
	public static boolean insert(OrderDetailDTO data) {
		try {
			// POST request to <service>/SaveVehicle
			HttpPost request = new HttpPost(new URI(Utils.SERVICE_URI
					+ "/OrderDetail_Insert"));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			// Build JSON string
			JSONStringer input = new JSONStringer()
				.object()
					.key("obj")
						.object()
							.key("IDOrder").value(Integer.valueOf(data.getIDOrder()))
							.key("IDFood").value(Integer.valueOf(data.getIDFood()))
							.key("Quantity").value(Integer.valueOf(data.getQuantity()))
							.key("Price").value(Float.valueOf(data.getPrice()))
							.endObject()
						.endObject();
			
			StringEntity entity = new StringEntity(input.toString());
			request.setEntity(entity);

			// Send request to WCF service
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);

			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			Log.i("OrderDetailBUS insert", "Response: " + result);
			
			return Boolean.parseBoolean(result);

		} catch (Exception e) {
			Log.e("insertData", e.toString());
			return false;
		}
	}
}
